<!DOCTYPE html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="/user/login"  method="post">
    <h1>登录用户</h1>
    <P>
        <label for="account">登录账户：</label>
        <input type="text" name="account" id="account"/>
    </P>
    <P>
        <label for="password">登录密码：</label>
        <input type="password" name="password" id="password"/>
    </P>
    <P>
        <label></label>
        <input type="submit" value="登录"/>
    </P>
    <script src="js/jquery-3.6.0.js"></script>

    <script>

        $(function(){
            $(":submit").on("click",function (event){
                event.preventDefault();
            if(checkAccount()&&checkPassword()) {
                $.ajax({
                    url: "User.do",
                    data: {
                        method: "login",
                        account: $("#account").val(),
                        password: $("#password").val()
                    },
                    type: "post",
                    success: function (result) {
                        if (result.code==200) {
                            console.log(result)
                            window.sessionStorage.setItem("user",JSON.stringify(result.data));
                            location.href="index.jsp";
                            alert("登陆成功");
                        } else {
                            alert("用户名或密码错误！")
                        }
                    }
                });
            };
            });
        });
        function checkAccount(){
            if($("#account").val()==''){
                alert("登录名称不能为空！");
                $("#account").focus();
                return false;
            }
            return true;
        }
        function checkPassword(){
            if($("#password").val()==''){
                alert("登录密码不能为空！");
                $("#password").focus();
                return false;
            }
            return true;
        }
    </script>
</form>
</body>
</html>
